package com.sleep.drug_traceability.controller;

import com.sleep.drug_traceability.service.UserService;
import com.sleep.drug_traceability.vo.ErrorCode;
import com.sleep.drug_traceability.vo.Result;
import com.sleep.drug_traceability.vo.UserVo;
import com.sleep.drug_traceability.vo.params.OneParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Sleep-6000
 * @date 2022/4/25
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 获取当前用户
     *
     * @param token token
     * @return 当前用户
     */
    @GetMapping("currentUser")
    public Result getCurrentUser(@RequestHeader("Authorization") String token) {
        UserVo userVo = userService.getCurrentUser(token);
        if (userVo == null) {
            return Result.fail(ErrorCode.TOKEN_ERROR.getCode(), ErrorCode.TOKEN_ERROR.getMsg());
        } else {
            return Result.success(userVo);
        }
    }

    /**
     * 获取所有药品生产商和销售商用户信息
     * @return 药品生产商和销售商用户信息
     */
    @GetMapping("getUser")
    public Result getUserTable() {
        return userService.getUserTable();
    }

    /**
     * 启用账号
     * @param param param
     * @return Result
     */
    @PostMapping("enable")
    public Result enable(@RequestBody OneParam param) {
        return userService.enable(param);
    }

    /**
     * 禁用账号
     * @param param param
     * @return Result
     */
    @PostMapping("disable")
    public Result disable(@RequestBody OneParam param) {
        return userService.disable(param);
    }
}
